package 链表和树.二叉树;
//Java：翻转二叉树
public class InvertBinaryTree{
    public static void main(String[] args) {
        Solution solution = new InvertBinaryTree().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)
//    public class TreeNode {
//        int val;
//        TreeNode left;
//        TreeNode right;
//        TreeNode() {}
//        TreeNode(int val) { this.val = val; }
//        TreeNode(int val, TreeNode left, TreeNode right) {
//            this.val = val;
//            this.left = left;
//            this.right = right;
//        }
//    }
class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root == null){
            return null;//递归的终止条件，防止无限递归下去
        }

        TreeNode left = invertTree(root.left);// 翻转左子树
        TreeNode right = invertTree(root.right); // 翻转右子树
        root.left = right;
        root.right = left;
        return root;//返回root是为了返回到上一层进行左右节点的交换
        //需要将这个已经处理好的子树的根节点返回给上一层调用
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}